﻿using System.Collections.Generic;
using System.Linq;
using Zadani09.db.DAO;

namespace Zadani09.db.DAOimpl
{
    public class UmelecDAOmysql : AbstractDAO,IUmelecDAO
    {

        public UmelecDAOmysql():base()
        {

        }

        public void create(Umelec umelec)
        {
            initiateConnection();

            db.Umelec.InsertOnSubmit(umelec);
            db.SubmitChanges();

            closeConnection();
        }

        public Umelec load(string id)
        {
            initiateConnection();

            uint idu = uint.Parse(id);

            var result = from u in db.Umelec
                         where u.IDUmelec == idu
                         select new Umelec()
                         {

                            IDUmelec = u.IDUmelec,
                            Jmeno = u.Jmeno,
                            Prijmeni = u.Prijmeni,
                            Popis = u.Popis,
                            DlouhyPopis = u.DlouhyPopis,
                            Mesto = u.Mesto,
                            Ulice = u.Ulice,
                            Popisne = u.Popisne,
                            PSC = u.PSC,
                            
                         };


            closeConnection();

            Umelec nahranyUmelec = null;

            List<Umelec> list = new List<Umelec>();

            foreach (var item in result)
            {
                list.Add(item);
            }


            if (list.Count > 1 || list.Count < 1)
            {
                //throw exeption nejakou...
                nahranyUmelec = null;
            }
            else
            {
                nahranyUmelec = list.First<Umelec>();
            }

            return nahranyUmelec;
        }

     
        public void delete(Umelec umelec)
        {
            initiateConnection();

            db.Umelec.Attach(umelec);
            db.Umelec.DeleteOnSubmit(umelec);

            db.SubmitChanges();

            closeConnection();
        }

        public void delete(string id)
        {
            Umelec umelec = new Umelec();
            umelec.IDUmelec = uint.Parse(id);
           
            this.delete(umelec);
          
        }


        public void update(Umelec novy)
        {
            initiateConnection();

            uint id = novy.IDUmelec;

            var updateQ = db.Umelec.First(e => e.IDUmelec == id);

            updateQ.Jmeno = novy.Jmeno;
            updateQ.Prijmeni = novy.Prijmeni;
            updateQ.Popis = novy.Popis;
            updateQ.DlouhyPopis = novy.DlouhyPopis;
            updateQ.Mesto = novy.Mesto;
            updateQ.Ulice = novy.Ulice;
            updateQ.Popisne = novy.Popisne;
            updateQ.PSC = novy.PSC;
                    
            db.SubmitChanges();

            closeConnection();
        }

  
        public List<Umelec> loadList(int odkud, int kolik)
        {
            initiateConnection();

            var result = from u in db.Umelec select u;

            List<Umelec> list = result.ToList<Umelec>();

            closeConnection();

            return list;
        }

   }
}
